package com.atguigu.web;

import com.atguigu.pojo.Book;
import com.atguigu.pojo.Cart;
import com.atguigu.pojo.CartItem;
import com.atguigu.service.BookService;
import com.atguigu.service.Impl.BookServiceImpl;
import com.atguigu.utils.WebUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @ClassName CartServlet
 * @Description 购物车模块
 * @Author 周轩霆
 * @Date 2022/4/24 20:38
 **/

public class CartServlet extends BaseServlet {
    private BookService bookService = new BookServiceImpl();

    /**
     * @return void
     * @Author 周轩霆
     * @Description 加入购物车
     * @Date 13:28 2022/4/26
     * @Param [req, resp]
     */
    protected void addItem(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取请求的参数 商品编号
        int id = WebUtils.parseInt(req.getParameter("id"), 0);
        //调用 bookService.queryBookById()得到图书的信息
        Book book = bookService.queryBookById(id);
        //返把图书信息，转化成CartItem商品项
        CartItem cartItem = new CartItem(book.getId(), book.getName(), 1, book.getPrice(), book.getPrice());
        //使用Cart.addItem(CartItem)添加商品项
        Cart cart = (Cart) req.getSession().getAttribute("cart");
        if (cart == null) {
            //保证用的是同一个购物车
            cart = new Cart();
            req.getSession().setAttribute("cart", cart);
        }
        cart.addItem(cartItem);
        System.out.println(cart);
        //重定向到商品列表界面
        //把地址一起发给服务器，重定向的时候保证独特性，即加入购物车之前是哪个页面，点击之后，返回到以前的页面，从哪里来的，就回到哪里去
        //例如:打印出：请求头Referer的值http://localhost:8080/book/client/bookServlet?action=page&pageNo=3
        System.out.println("请求头Referer的值"+req.getHeader("Referer"));
        resp.sendRedirect(req.getHeader("Referer"));
        //最后一个添加的商品S
        req.getSession().setAttribute("lastName",cartItem.getName());

    }
    /**
     * @Author 周轩霆
     * @Description 删除商品项
     * @Date 15:56 2022/4/26
     * @Param [req, resp]
     * @return void
    */
    protected void deleteItem(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取商品编号
        int id = WebUtils.parseInt(req.getParameter("id"), 0);
         //获取购物车对象
        Cart cart = (Cart) req.getSession().getAttribute("cart");
        if(cart!=null){
            cart.deleteItem(id);
            resp.sendRedirect(req.getHeader("Referer"));
        }


    }
    /**
     * @Author 周轩霆
     * @Description 清空购物车
     * @Date 16:12 2022/4/26
     * @Param [req, resp]
     * @return void
    */
    protected void clear(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //获取购物车对象
        Cart cart = (Cart) req.getSession().getAttribute("cart");
        if(cart!=null){
            cart.clear();
            resp.sendRedirect(req.getHeader("Referer"));
        }

    }
    /**
     * @Author 周轩霆
     * @Description 修改购物车中商品的数量
     * @Date 16:48 2022/4/26
     * @Param [req, resp]
     * @return void
    */
    protected void updateCount(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //获取商品编号
        int id = WebUtils.parseInt(req.getParameter("id"), 0);
        //获取商品数量
        int count = WebUtils.parseInt( req.getParameter("count"),0);
        //获取购物车对象
        Cart cart = (Cart) req.getSession().getAttribute("cart");
        if(cart!=null){
            cart.updateCount(id,count);
            resp.sendRedirect(req.getHeader("Referer"));
        }


    }


    }
